package com.wnx.job.modules.pms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.dto.FavorParam;
import com.wnx.job.modules.pms.model.PmsFavor;
import com.wnx.job.modules.pms.service.PmsFavorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/pms/pmsFavor")
@Api(tags = "FavorController",description = "收藏喜爱管理")
public class PmsFavorController {

    @Resource
    private JmsStudentService studentService;
    @Resource
    private PmsFavorService favorService;


    /**
     * 收藏企业
     * @return
     */
    @GetMapping("/companySave/{id}")
    @ApiOperation("收藏企业")
    public ResultData<String> saveCompany(@PathVariable(name = "id")Long companyId, Principal principal){
        log.info("========系统正在收藏企业中========");
        //获取前端的企业ID

        //获取学生ID
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);

        //构造收藏喜欢对象
        PmsFavor favor = new PmsFavor();
        favor.setStudentId(student.getId());
        favor.setCompanyId(companyId);
        favor.setFavorDate(new Date());

        //调用持久层，进行收藏企业
        favorService.saveCompany(favor);
        log.info("========系统成功收藏企业========");

        //返回结果对象
        return ResultData.success(Const.SAVE_FAVOR_COMPANY_SUCCESS);


    }


    /**
     * 收藏招聘信息
     * @return
     */
    @GetMapping("/postSave/{id}")
    @ApiOperation("收藏招聘信息")
    public ResultData<String> savePost(@PathVariable(name = "id")Long postId, Principal principal){
        log.info("========系统正在收藏招聘信息中========");
        //获取前端的企业ID

        //获取学生ID
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);

        PmsFavor favor = new PmsFavor();
        favor.setStudentId(student.getId());
        favor.setPostId(postId);
        favor.setFavorDate(new Date());
        favorService.savePost(favor);
        log.info("========系统成功收藏招聘信息中========");
        return ResultData.success(Const.SAVE_FAVOR_POST_SUCCESS);

    }


    /***
     * 查看收藏的公司
     * @return
     */
    @PostMapping("/getFavorCompany")
    @ApiOperation("查看收藏的公司")
    public ResultData<PageResult<FavorParam>> getFavorCompany(@RequestBody QueryPageBean queryPageBean, Principal principal){
        log.info("========系统正在查看收藏的公司中========");
        JmsStudent student = studentService.findByAccount(principal.getName());
        PageResult<FavorParam> pageResult =  favorService.getFavorCompany(queryPageBean,student.getId());
        log.info("========系统成功查看收藏的公司========");
        return ResultData.success(Const.QUERY_FAVOR_COMPANY_SUCCESS,pageResult);

    }

    /**
     * 查看收藏的招聘信息
     * @param queryPageBean
     * @param principal
     * @return
     */
    @PostMapping("/getFavorPost")
    @ApiOperation("查看收藏的招聘信息")
    public ResultData<PageResult<FavorParam>> getFavorPost(@RequestBody QueryPageBean queryPageBean, Principal principal){
        log.info("========系统正在查看收藏的招聘信息中========");
        JmsStudent student = studentService.findByAccount(principal.getName());
        PageResult<FavorParam> pageResult =  favorService.getFavorPost(queryPageBean,student.getId());
        log.info("========系统成功查看到收藏的招聘信息========");
        return ResultData.success(Const.QUERY_FAVOR_POST_SUCCESS,pageResult);
    }


    /**
     * 根据ID删除收藏的喜欢信息
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("根据ID删除收藏的喜欢信息")
    public ResultData<String> deleteFavor(@PathVariable(name = "id")Long id){
        log.info("========系统正在根据ID删除喜欢的职位信息中========");
        favorService.removeById(id);
        log.info("========系统成功根据ID删除喜欢的职位信息========");
        return ResultData.success(Const.DELETE_FAVOR_SUCCESS);

    }





}

